import { createStore, useStore as useVuexStore, Store } from "vuex";
import moduleUser from "./user";
import modulePicUpload from "./picUpload";
import { IState, IStoreType, LSModule } from "./types";

import moduleTest from "./test";
import { moduleScriptList } from "./scriptList"
const store = createStore<IState>({
  state() {
    return {};
  },
  mutations: {},
  actions: {},
  modules: {
    moduleUser,
    moduleTest,
    modulePicUpload,
    moduleScriptList
  },
});

//加载所有模块的同步和异步方法(暂时不包括getters) mutations actions

function defineObjProp(obj: any, name: string) {
  let o1 = {};
  for (let k in obj) {
    Object.defineProperty(o1, k, {
      configurable: true,
      enumerable: true,
      value: `${name}/${k}`,
      writable: true,
    });
  }
  return o1;
}

function load(objList: any[]) {
  let o1: any = {};
  let o2: any = {};
  objList.forEach((d) => {
    for (let key in d) {
      if (d[key] instanceof Object) {
        o1[key] = defineObjProp(d[key], d.name);
      }
    }
    o2[d.name] = { ...o1 };
    o1 = {};
  });

  return o2;
}

type II = Store<IStoreType> & LSModule;

export function useStore(): II {
  const res = load([moduleUser, moduleTest]);

  const ls: LSModule = {
    LSModules: {
      ...res,
    },
  };
  return Object.assign(useVuexStore(), ls);
}

export default store;
